﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GUESTSMANAGEMENT.Controller;
using System.Collections.Generic;
using Coolite.Ext.Web;

namespace GUESTSMANAGEMENT.View.ListsGuests
{
    public partial class frmInvite : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public Store GridStore
        {
            get;
            set;
        }


        protected void FormSave(object sender, AjaxEventArgs e)
        {
            try
            {
                int cdUser = Convert.ToInt32(Session["userCd"]);
                gmuser user = Controller.User.getByUser(cdUser);

                int evento = (int)Session["eventId"];
                int guestId = (int)Session["guestId"];
                string guestName = Session["guestName"].ToString();
                string guestMail = Session["guestMail"].ToString();

                gmevent eventoCompare = Controller.Event.getByEvent(evento);

                int typeInvite = int.Parse(this.cmbInvite.SelectedItem.Value);
                int qtdInvite = 0;
                string msg = "";

                List<gmlistguests> listGuestsInvites = Controller.GuestsList.getByListsGuestsInvites(evento, cdUser, typeInvite);

                switch (typeInvite)
                {
                    case 1: qtdInvite = (int)eventoCompare.QTBF;
                        msg = "Bônus Feminino";
                        break;
                    case 2: qtdInvite = (int)eventoCompare.QTBM;
                        msg = "Bônus Masculino";
                        break;
                    case 3: qtdInvite = (int)eventoCompare.QTFF;
                        msg = "Free Feminino";
                        break;
                    case 4: qtdInvite = (int)eventoCompare.QTFM;
                        msg = "Free Masculino";
                        break;
                }

                if ((qtdInvite - listGuestsInvites.Count) > 0)
                {
                    using (guestsmanagementEntities context = new guestsmanagementEntities())
                    {

                        gmlistguests listsGuests = new gmlistguests();
                        listsGuests.GMEVENT_CDEVENT = evento;
                        //listsGuests.GMGUEST_CDGUEST = guestId;
                        listsGuests.GMUSER_CDUSER = cdUser;
                        listsGuests.NMGUEST = guestName;
                        listsGuests.NMMAILGUEST = guestMail;
                        listsGuests.FGTYPEINVITE = typeInvite;
                        Controller.GuestsList.addListsGuests(listsGuests);
                        this.cmbInvite.ClearCls.ToString();
                        this.GridStore.AddScript(string.Format("{0}.reload();", this.GridStore.ClientID));
                        this.winGuestInvite.Hide();
                        
                    }
                }
                else
                {
                    Ext.Msg.Alert("Alerta!", "Quantidade de <b>" + msg + "</b> Esgotado!").Show();
                }
            }
            catch
            {
                Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
            }
        }

        public void ShowGuests()
        {
            this.winGuestInvite.Show();
        }


        protected void Close(object sender, AjaxEventArgs e)
        {
            this.winGuestInvite.Hide();
            ClearWindow(Controls);
        }

        public void ClearWindow(ControlCollection AllControls)
        {
            foreach (Control c in AllControls)
            {
                if (c is TextField)
                {
                    ((TextField)c).Text = "";
                }
                if (c is ComboBox)
                {
                    ((ComboBox)c).Value = null;
                }
                if (c is Checkbox)
                {
                    ((Checkbox)c).Checked = true;
                }
                if (c is Hidden)
                {
                    ((Hidden)c).Text = "";
                }
                ClearWindow(c.Controls);
            }
        }
    }
}